package com.rpj.stauy.controller;

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisListResult;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.NoApiKeyException;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.internal.Json;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.output.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Name;
import java.io.IOException;
import java.util.Base64;

@RestController
@Slf4j
public class ImageAicontroller {

    @Autowired
    @Qualifier("chatimage")
    private WanxImageModel wanxImageModel;
    @Autowired
    @Qualifier("chat")
    private ChatModel chatModel;

    @Value("classpath:static/images/下载.jpg")
    public Resource resource;

    //http://localhost:9094/image/text
    @GetMapping("image/text")
    public String imageChat(@RequestParam(value = "prompt",defaultValue = "你是谁") String prompt){

        try {
            //图片转换为字符串
            byte[] contentAsByteArray = resource.getContentAsByteArray();
            String encoder = Base64.getEncoder().encodeToString(contentAsByteArray);
            UserMessage userMessage = UserMessage.from(TextContent.from("图片中是什么鸟"), ImageContent.from(encoder,"image/jpeg"));
            return chatModel.chat(userMessage).aiMessage().text();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    //http://localhost:9094/image/text2
    @GetMapping("image/text2")
    public String imageChat2(@RequestParam(value = "prompt",defaultValue = "你是谁") String prompt){
        Response<Image> generate = wanxImageModel.generate("美女");
        return generate.content().url().toString();
    }



    //http://localhost:9094/image/text3
    @GetMapping("image/text3")
    public String imageChat3(){
        String prompt = "道奇，地狱猫，车展";
        ImageSynthesisParam aliQwenApi = ImageSynthesisParam.builder()
                .apiKey(System.getenv("aliQwen_api"))
//                .modelName("wanx2.1-t2i-turbo")
                .model(ImageSynthesis.Models.WANX_V1)
                .prompt(prompt)
                .style("<watercolor>")
                .n(1)//??
                .size("1024*1024")
                .build();
        ImageSynthesis imageSynthesis = new ImageSynthesis();
        ImageSynthesisResult imageSynthesisResult  = null;

        System.out.println("---sync call, please wait a moment----");
        try {
            imageSynthesisResult= imageSynthesis.call(aliQwenApi);
        } catch (NoApiKeyException e) {
            throw new RuntimeException(e);
        }
        return Json.toJson(imageSynthesisResult);

    }
}
